package com.jtny.nytb.datafilling.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jtny.component.dto.CurrentUserDTO;
import com.jtny.component.entity.LocalHolder;
import com.jtny.component.entity.PageData;
import com.jtny.component.entity.PageParam;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.BeanUtils;
import com.jtny.component.util.CurrentUserUtils;
import com.jtny.nytb.datafilling.dao.DatafillingUserelectricFillDAO;
import com.jtny.nytb.datafilling.dao.UserelectricEquipmentDAO;
import com.jtny.nytb.datafilling.domain.DatafillingUserelectircFillDO;
import com.jtny.nytb.datafilling.domain.InsertUserEletricParamVO;
import com.jtny.nytb.datafilling.domain.UserelectricEquipmentDO;
import com.jtny.nytb.datafilling.service.UserElectircService;
import com.jtny.nytb.datafilling.dto.DatafillingUserelectircFillDTO;
import com.jtny.nytb.datafilling.service.DatafillingUserElectircService;
import com.jtny.nytb.datafilling.vo.UserEletricParamVO;
import com.jtny.nytb.datafilling.vo.UserelectricEquipmentVO;
import com.jtny.nytb.equipment.domain.equipment.UserEquipmentRelationDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 分时时段配置填报管理bizImpl类
 *
 * @author 智慧能源中心-罗霄林
 * @version 1.0
 * @package com.jtny.nytb.energymonitor.datafilling.biz.impl
 * @fileName PeriodTimeBizImpl.java
 * @createTime 2023年09月25日 17:11
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@Service
public class UserElectircServiceImpl implements UserElectircService {

    @Autowired
    private DatafillingUserElectircService datafillingUserElectircService;

    @Autowired
    private DatafillingUserelectricFillDAO datafillingUserelectricFillDAO;

    @Autowired
    private UserelectricEquipmentDAO userelectricEquipmentDAO;

    @Override
    public Boolean addUserEletricInfo(UserEletricParamVO userEletricParamVO) {
        DatafillingUserelectircFillDTO dto = BeanUtils.copyToObject(userEletricParamVO, DatafillingUserelectircFillDTO.class);
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        CurrentUserUtils.setCreateDefaultField(dto, currentUserDTO);
        return datafillingUserElectircService.addUserElectricInfo(dto);
    }

    @Override
    public Boolean saveUserEletricInfo(UserEletricParamVO userEletricParamVO) {
        DatafillingUserelectircFillDTO dto = BeanUtils.copyToObject(userEletricParamVO, DatafillingUserelectircFillDTO.class);
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        CurrentUserUtils.setCreateDefaultField(dto, currentUserDTO);
        return datafillingUserElectircService.saveUserElectricInfo(dto);
    }

    @Override
    public PageData<DatafillingUserelectircFillDTO> queryPage(PageParam pageParam) {
        return datafillingUserElectircService.queryPage(pageParam);
    }

    @Override
    public List<String> queryPowerFactor() {
        return datafillingUserElectircService.queryPowerFactor();
    }

    @Override
    public void delUserEletricInfo(String id) {
        datafillingUserelectricFillDAO.deleteById(id);
    }

    @Override
    public String insertUserEletricInfo(InsertUserEletricParamVO insertUserEletricParamVO) {
        if ("0".equals(insertUserEletricParamVO.getFlag())){
            DatafillingUserelectircFillDTO filldto = getUserElectricInfoByYearMonth(insertUserEletricParamVO.getFillTime());
            if (ObjectUtil.isNotEmpty(filldto))
                return "repeatData";
        }
        DatafillingUserelectircFillDTO dto = queryUserElectricInfoById(insertUserEletricParamVO.getId());
        dto.setFillTime(insertUserEletricParamVO.getFillTime());
        dto.setId(null);
        CurrentUserDTO currentUserDTO = LocalHolder.getCurrentUser();
        CurrentUserUtils.setCreateDefaultField(dto, currentUserDTO);
        saveUserElectricInfo(dto);
        return "success";
    }

    public DatafillingUserelectircFillDTO getUserElectricInfoByYearMonth(String yearMonth) throws ServiceException {
        LambdaQueryWrapper<DatafillingUserelectircFillDO> queryWrapper = Wrappers.lambdaQuery(DatafillingUserelectircFillDO.class)
                .eq(DatafillingUserelectircFillDO::getFillTime,yearMonth);
        DatafillingUserelectircFillDO datafillingUserelectircFillDO = datafillingUserelectricFillDAO.selectOne(queryWrapper);
        if (ObjectUtils.isNotEmpty(datafillingUserelectircFillDO))
            return BeanUtils.copyToObject(datafillingUserelectircFillDO,DatafillingUserelectircFillDTO.class);
        else
            return null;
    }

    public DatafillingUserelectircFillDTO queryUserElectricInfoById(String id) {
        return BeanUtils.copyToObject(datafillingUserelectricFillDAO.selectById(id),DatafillingUserelectircFillDTO.class);
    }

    public Boolean saveUserElectricInfo(DatafillingUserelectircFillDTO datafillingUserelectircFillDTO) {
        String yearMonth = "";
        if (StringUtils.isEmpty(datafillingUserelectircFillDTO.getFillTime())){
            yearMonth = new SimpleDateFormat("yyyy-MM").format(new Date());
        }else {
            yearMonth = datafillingUserelectircFillDTO.getFillTime();
        }
        LambdaQueryWrapper<DatafillingUserelectircFillDO> queryWrapper = Wrappers.lambdaQuery(DatafillingUserelectircFillDO.class)
                .eq(DatafillingUserelectircFillDO::getFillTime,yearMonth);
        DatafillingUserelectircFillDO datafillingUserelectircFillDO = datafillingUserelectricFillDAO.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(datafillingUserelectircFillDO)){
            datafillingUserelectircFillDO = BeanUtils.copyToObject(datafillingUserelectircFillDTO,DatafillingUserelectircFillDO.class);
            datafillingUserelectircFillDO.setFillTime(yearMonth);
            return datafillingUserelectricFillDAO.insert(datafillingUserelectircFillDO)==1;
        }else {
            DatafillingUserelectircFillDO update = BeanUtils.copyToObject(datafillingUserelectircFillDTO,DatafillingUserelectircFillDO.class);
            update.setId(datafillingUserelectircFillDO.getId());
            return datafillingUserelectricFillDAO.updateById(update)==1;
        }
    }

    @Override
    public void saveOrUpdateRealtion(List<UserelectricEquipmentVO> list) {
        List<UserelectricEquipmentDO> userelectricEquipmentDOS = BeanUtils.copyToList(list, UserelectricEquipmentDO.class);
        for(UserelectricEquipmentDO userelectricEquipmentDO:userelectricEquipmentDOS){
                LambdaQueryWrapper<UserelectricEquipmentDO> wrapper=new LambdaQueryWrapper<UserelectricEquipmentDO>()
                    .eq(UserelectricEquipmentDO::getEquipmentId,userelectricEquipmentDO.getEquipmentId())
                    .eq(UserelectricEquipmentDO::getUserelectricId,userelectricEquipmentDO.getUserelectricId());
                if(userelectricEquipmentDAO.selectList(wrapper).isEmpty()){
                    userelectricEquipmentDAO.insert(userelectricEquipmentDO);
                }else{
                    LambdaQueryWrapper<UserelectricEquipmentDO> wrapper2=new LambdaQueryWrapper<UserelectricEquipmentDO>()
                            .eq(UserelectricEquipmentDO::getUserelectricId,userelectricEquipmentDO.getUserelectricId())
                            .eq(UserelectricEquipmentDO::getEquipmentId,userelectricEquipmentDO.getEquipmentId());
                    userelectricEquipmentDAO.update(userelectricEquipmentDO,wrapper2);
                }

        }
    }

    @Override
    public List<UserelectricEquipmentDO> getUserelectricEquipment(Long id) {
        LambdaQueryWrapper<UserelectricEquipmentDO> wrapper = new LambdaQueryWrapper<UserelectricEquipmentDO>()
                .eq(UserelectricEquipmentDO::getUserelectricId,id);
        return userelectricEquipmentDAO.selectList(wrapper);
    }

    @Override
    public void removeRelation(List<UserelectricEquipmentVO> ids) {
        List<UserelectricEquipmentDO> list=BeanUtils.copyToList(ids, UserelectricEquipmentDO.class);
        for(UserelectricEquipmentDO userelectricEquipmentDO:list){
            LambdaQueryWrapper<UserelectricEquipmentDO> wrapper = new LambdaQueryWrapper<UserelectricEquipmentDO>()
                    .eq(UserelectricEquipmentDO::getUserelectricId,userelectricEquipmentDO.getUserelectricId())
                    .eq(UserelectricEquipmentDO::getEquipmentId,userelectricEquipmentDO.getEquipmentId());
            userelectricEquipmentDAO.delete(wrapper);
        }
    }


}
